# ======================================================================
#
# Copyright (C) ADN
# Clean.pl
# Description:
#  - Netoie fichier de processus
#  - Clean process file
#
# ======================================================================

use strict;
my @proc;
my ( $file );

# #######################
# DEFINE THE PATH OF PROCESS
# #######################
if ( $^O eq 'MSWin32' ) {
   $file = $ENV{CommonProgramFiles} . '/ADN/';
}
elsif ( $^O eq 'linux' || $^O eq 'darwin' ) {
   $file = '/tmp/ADN/';
}
$file .= 'process';
@proc = _init();
clean();
exit;

# #######################
# INITIALISE PROCESS ARRAY
# #######################
sub _init {
    if (open List, '<', $file) {
      while ( <List> ) {
    	 chomp;
	     if ( $_ ne '' ) {
	       push @proc, $_;
	     }
      }
      if (!close List) {}
    }
    return @proc;
}

# #######################
# CLEAN THE FILE PROCESS
# #######################
sub clean {
    my $check;
    my @tab;
    if (open WRITER, '>', $file) {
      foreach ( @proc ) {
	     if ( $_ ne '' ) {
	       @tab = split /\//smx, $_;
	       $check = kill 0, $tab[2];
	       if ( $check == 1 ) {
		        print WRITER $_ . "\n";
	       }
	     }
      }
      if (!close WRITER) {}
    }
    return;
}
__END__
